#--
# Copyright (c) 2007 Ryan Grove <ryan@wonko.com>
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
#   * Redistributions of source code must retain the above copyright notice,
#     this list of conditions and the following disclaimer.
#   * Redistributions in binary form must reproduce the above copyright notice,
#     this list of conditions and the following disclaimer in the documentation
#     and/or other materials provided with the distribution.
#   * Neither the name of this project nor the names of its contributors may be
#     used to endorse or promote products derived from this software without
#     specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#++

module DenySpam; module Util

  # Provides methods for performing Realtime Blackhole List lookups of IP
  # addresses. Results are cached for (by default) six hours to improve
  # performance and to avoid wasting the resources of RBL providers.
  module RBL

    CACHE_TIME = 21600
    
    @cache = Hash.new do |hash, lookup_addr|
      hash[lookup_addr] = {
        :result  => false,
        :expires => Time.now + CACHE_TIME
      }
    end

    # Flushes expired results from the cache. Results are considered expired
    # when they're at least six hours old.
    def self.clean_cache
      now = Time.now
      @cache.delete_if {|lookup_addr, value| value[:expires] <= now }
    end

    # Looks up an IP address using the specified custom RBL address.
    def self.listed?(addr, rbl)
      lookup_addr = addr.sub(/^((\d+)\.(\d+)\.(\d+)\.(\d+))$/, '\5.\4.\3.\2.' +
          rbl)

      return @cache[lookup_addr] if @cache.include?(lookup_addr)
      @cache[lookup_addr][:result] = Resolv.getaddress(lookup_addr)

    rescue
      @cache[lookup_addr][:result] = false
    end

    def self.ahbl?(addr);    listed?(addr, 'dnsbl.ahbl.org');       end
    def self.dsbl?(addr);    listed?(addr, 'list.dsbl.org');        end
    def self.njabl?(addr);   listed?(addr, 'combined.njabl.org');   end
    def self.sbl?(addr);     listed?(addr, 'sbl.spamhaus.org');     end
    def self.sbl_xbl?(addr); listed?(addr, 'sbl-xbl.spamhaus.org'); end
    def self.sorbs?(addr);   listed?(addr, 'dnsbl.sorbs.net');      end
    def self.spamcop?(addr); listed?(addr, 'bl.spamcop.net');       end
    def self.xbl?(addr);     listed?(addr, 'xbl.spamhaus.org');     end

  end

end; end
